<?php
/*******************************************************************************
Rd/Course.php
Implements Course utilities for RD

Created by Troy Hurteau, NCSU Libraries, NC State University (libraries.opensource@ncsu.edu).

This file is part of NCSU's distribution of ReservesDirect. This version has not been downloaded from Emory University
or the original developers of ReservesDirect. Neither Emory University nor the original developers of ReservesDirect have authorized
or otherwise endorsed or approved this distribution of the software.

Copyright (c) 2004-2006 Emory University, Atlanta, Georgia.

Licensed under the NCSU ReservesDirect License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. You may obtain a copy of the full License at
 http://www.lib.ncsu.edu/it/opensource/

ReservesDirect is distributed in the hope that it will be useful,
but is distributed "AS IS" and WITHOUT ANY WARRANTY, without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE, and without any warranty as to non-infringement of any third
party's rights. See the License for the specific language governing permissions and limitations under the License.

The original version of ReservesDirect is located at:
http://www.reservesdirect.org/

This version of ReservesDirect, distributed by NCSU, is located at:
http://code.google.com/p/reservesdirect-ncsu/

*******************************************************************************/

require_once(APPLICATION_PATH . '/lib/Rd/Dao/Course.php');
require_once(APPLICATION_PATH . '/lib/Rd/Dao/Course/Instance.php');
require_once(APPLICATION_PATH . '/lib/Rd/Dao/Department.php');
require_once(APPLICATION_PATH . '/lib/Rd/Dao/User.php');

/**
 * 
 * Course utility object.
 * @author jthurtea
 *
 */

class Rd_Course{
	
	protected static $_pickerPrerequisets = array(
		'searchTerm[]',
		'searchTerm',
		'searchInstructor',
		'searchInstructorResult',
		'searchDepartment',
		'searchDepartmentResult',
		'searchCourse',
		'searchCourseResult'
	);
	
	public static function getById($courseId)
	{
		return NULL;
	}
	
	public static function getByDeptIdAndCourseNumber($departmentId, $courseNumber)
	{
		return NULL;
	}
	
	public static function pickerSearchReady($request = NULL)
	{
		if (is_null($request)) {
			$request = $_REQUEST;
		}
		foreach(self::$_pickerPrerequisets as $requirementOptions) {
			if(Rd_Array::keyExistsAndNotBlank($requirementOptions, $request)) {
				return true;
			}
		}
		return false;
	}
	
	public static function pickerSearch($request = NULL)
	{
		if (is_null($request)) {
			$request = $_REQUEST;
		}
		$term = Rd_Array::keyExistsAndNotBlank('searchTerm', $request)
			? $request['searchTerm']
			: '*';
		$instructor = Rd_Array::keyExistsAndNotBlank('searchInstructorResult', $request)
			? $request['searchInstructorResult']
			: (
				Rd_Array::keyExistsAndNotBlank('searchInstructor', $request)
				? self::pickerLookupInstructor($request['searchInstructor'])
				: '*'
			);
		$department = //Rd_Array::keyExistsAndNotBlank('searchDepartmentResult', $request)
			//? $request['searchDepartmentResult']
			//: ( // #TODO this isn't normalized in the DB yet.
				Rd_Array::keyExistsAndNotBlank('searchDepartment', $request)
				? self::pickerLookupDepartment($request['searchDepartment'])
				: '*'
			;//);
		$course = //Rd_Array::keyExistsAndNotBlank('searchCourseResult', $request)
			//? $request['searchCourseResult']
			//: ( // #TODO this isn't normalized in the DB yet.
				Rd_Array::keyExistsAndNotBlank('searchCourse', $request) 
				? self::pickerLookupCourse($request['searchCourse'])
				: '*'
			;//);
		$status = 
			Rd_Array::keyExistsAndNotBlank('searchStatus', $request) 
			? $request['searchStatus']
			: '*';
		if($instructor === FALSE) {
			Rd_Layout::setMessage('instructorMatchError', 'No Matching Instructors Found');
			$instructor = '*';
		}
		if($department === FALSE) {
			Rd_Layout::setMessage('departmentMatchError', 'No Matching Departments Found');
			$department = '*';
		}
		if($course === FALSE) {
			Rd_Layout::setMessage('courseMatchError', 'No Matching Courses Found');
			$course = '*';
		}
		if(
			'*' == $term 
				&& (
					('*' == $course && '*' == $department)
					|| ('*' == $course && '*' == $instructor)
					|| ('*' == $instructor && '*' == $department)
				)
		) {
			Rd_Layout::appendMessage('generalAlert', 'If you are searching All Terms, please provide at least two other search criteria.');
			return array();
		} else if (
			'*' == $course
			&& '*' == $department
			&& '*' == $instructor
		) {
			Rd_Layout::appendMessage('generalAlert', 'Please provide at least one other search criteria in addition to the term.');
			return array();
		}		

		$searchParams = array(
			'term' => $term,
			'instructor' => $instructor,
			'department' => $department,
			'course' => $course,
			'status' => $status
		);
		$cas =  Rd_Dao_Course_Instance::search($searchParams);
		$courses = array();
		foreach($cas as $ca){
			$courses[] = new course($ca);
		}
		return $courses;
	}
	
	public static function pickerLookupInstructor($search)
	{
		if ('' == trim($search)) {
			return array();
		}
		$results = Rd_Dao_User::nameAndUsernameLookup($search);
		if(!$results) {
			return false;
		}
		$map = array();
		foreach($results as $data) {
			$map[] = $data[0];
		}
		return $map;
	}
	
	public static function pickerLookupDepartment($search)
	{
		if ('' == trim($search)) {
			return array();
		}
		$results = Rd_Dao_Department::nameAndAbbreviationLookup($search);
		if(!$results) {
			return false;
		}
		$map = array();
		foreach($results as $data) {
			$map[] = $data[0];
		}
		return $map;
	}
	
	public static function pickerLookupCourse($search)
	{
		if ('' == trim($search)) {
			return array();
		}
		$results = Rd_Dao_Course::nameAndNumberLookup($search);
		if(!$results) {
			return false;
		}
		$map = array();
		foreach($results as $data) {
			$map[] = $data[0];
		}
		return $map;
	}
	
	public static function getFromCi($ci)
	{
		$courseInstance = new courseInstance($ci);
		return $courseInstance->getPrimaryCourse();
	}
	
	public static function coursesUserCanEdit($user)
	{
		$courses = array();
		if (is_a($user,'proxy')) {
			$cis = $user->getCourseInstancesToEdit();
			
			foreach($cis as $ci) {
				$crossListings = $ci->getCourses();
				foreach($crossListings as $crossListing) {
					if(!in_array($crossListing, $courses)) {
						$courses[] = $crossListing;
					}
				}
			}
		}
		return $courses;
	}
}